model.storage = {};

model.storage.wishes = new function() {
	
	var _wishes = new Array();
	
	var _offsets = new Array();
	
	var _limit = config.wishes_per_list;
	
	var _items = new Object();
	
	this.setList = function(user_id, data) {
		
		if (typeof _wishes[user_id] == "undefined") {
			_wishes[user_id] = new Array();
			_offsets[user_id]  = 0;
		}
				
		$.each(data, function(i, item) {
			
			if (utils.empty(_items[item.wish_id])) {
			
				item.tags_list = item.tags.join(", ");
				item.img_url = (item.img_url == '') ? (config.images_path + 'box' + (i % 2 + 1) + '.png') : item.img_url;
				if (item.reason_id < 1) item.reason_id = 1;
				item.reason = config.reasons[(item.reason_id - 1)].name;
				item.category = config.categories[(item.category_id)].name;
				
				if (model.storage.users.get(item.owner_id) != null)
					item.owner_name = model.storage.users.get(item.owner_id)["name"];
				item.satisfied = (item.status == 2);
				
				_items[item.wish_id] = item;
			} else {
				item = _items[item.wish_id];
			}
						
			_wishes[user_id].push(item);
		});
	};
	
	this.loaded = function(user_id) {
		return !utils.empty(_wishes[user_id]);
	};
	
	this.next = function(user_id) {
		return _offsets[user_id] = _offsets[user_id] + _limit;
	};
	
	this.prev = function(user_id) {
		if (_offsets[user_id] >= _limit) {
			_offsets[user_id] = _offsets[user_id] - _limit;
		}
	};
	
	this.getList = function(user_id, turn) {
		
		switch(turn) {
			case "next":
	        	this.next(user_id);
				break;
			case "prev":
	        	this.prev(user_id);
			    break;
			case "rewind":
				_offsets[user_id] = 0;
				break;
		}
		
		if (utils.empty(_wishes[user_id])) return false;
		
		var from = _offsets[user_id];
		var till = _offsets[user_id] + _limit;
		
		var items = _wishes[user_id].slice(from, till);
		
		return {"items" : items, "owner_id" : user_id, "page" : (_offsets[user_id] / _limit + 1), "total" : _wishes[user_id].length, "pages" : Math.ceil(_wishes[user_id].length / _limit)};
	};
	
	this.get = function(wish_id) {
		
		return _items[wish_id];
	};
	
	this.replace = function(user_id, data) {
		
		if (utils.empty(_wishes[user_id])) return;

		data.tags_list = data.tags.join(", ");
		data.img_url = (data.img_url == '') ? config.default_image : data.img_url;
		data.reason = config.reasons[(data.reason_id - 1)].name;
		data.category = config.categories[(data.category_id)].name;

		_items[data.wish_id] = data;
		
		// replace non empty fields
		$.each(_wishes[user_id], function(i, item) {
			if (item.wish_id == data.wish_id) {
				
				_wishes[user_id][i] = data;
				return false;
			}
		});
	};
	
	this.add = function(user_id, item) {
				
		if (utils.empty(item) || utils.empty(_wishes[user_id])) return;

		item.tags_list = item.tags;
		item.img_url = (item.img_url == '') ? config.default_image : item.img_url;

		item.reason = config.reasons[(item.reason_id - 1)].name;
		item.category = config.categories[(item.category_id)].name;
		
		item.owner_name = viewer["name"];
		
		_items[item.wish_id] = item;
		
		_wishes[user_id].unshift(item);
	};
	
	this.remove = function(user_id, wish_id) {

		$.each(_wishes[user_id], function(i, item) {
			if (item.wish_id == wish_id) {
				_wishes[user_id].splice(i, 1);
				return false;
			}
		});
	}
};

model.storage.users = new function() {
	
	var _users = Object();
	
	var _friends = null; 
	
	this.add = function(users) {
		
		users = utils.toList(users);
		
		$.each(users, function(i, user) {
			if (utils.empty(_users[user.id])) _users[user.id] = user;
		});
		
	};
	
	this.get = function(id) {
		return utils.empty(_users[id]) ? null : _users[id];
	};
	
	this.setFriends = function(friends) {
		_friends = friends;
	};
	
	this.getFriends = function() {
		return _friends;
	};
	
}
